package com.nf.health.app.widget.custom_chatrow;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.widget.chatrow.EaseChatRow;
import com.hyphenate.exceptions.HyphenateException;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.WebViewActivity;
import com.nf.health.app.utils.ActivityUtils;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by 恩福 on 2016/10/10.
 * 纯文消息 Item
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class EaseChatRowOnlyMessage extends EaseChatRow {

    private TextView tvTitle;
    private TextView tvContent;
    private String messageid;
    private String title;
    private String content;

    public EaseChatRowOnlyMessage(Context context, EMMessage message, int position, BaseAdapter adapter,int state) {
        super(context, message, position, adapter,state);
    }

    @Override
    protected void onInflatView() {
        if(message.direct() == EMMessage.Direct.RECEIVE){
            inflater.inflate(R.layout.ease_row_received_drug_alert,this);
        }
    }

    @Override
    protected void onFindViewById() {
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvContent = (TextView) findViewById(R.id.tv_content);
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onSetUpView() {

//        Glide.with(context).load(R.drawable.ic_msg_system).into(userAvatarView);

        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        try {
            JSONObject object = new JSONObject("{"+txtBody.getMessage()+"}");
            title = (String) object.get("title");
            content = (String) object.get("content");
            messageid = (String) object.get("messageid");
            tvTitle.setText(title);
            tvContent.setText(content);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if(!message.isAcked() && message.getChatType() == EMMessage.ChatType.Chat){
            try {
                EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
            } catch (HyphenateException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    protected void onBubbleClick() {

        if(!TextUtils.isEmpty(messageid)) {
            Bundle bundle = new Bundle();
            bundle.putString(WebViewActivity.KEY_URL, messageid);
            bundle.putBoolean(WebViewActivity.KEY_IS_SHARE, true);
            bundle.putString(WebViewActivity.KEY_SHARE_CONTENT, content);
            bundle.putString(WebViewActivity.KEY_TITLE, title);
            ActivityUtils.showActivity(context, WebViewActivity.class, bundle);
        }
    }
}
